package com.lap.auth.adapter.driving.persistence.menu.repository;

import com.lap.auth.adapter.driving.persistence.api.ApiCache;
import com.lap.auth.adapter.driving.persistence.menu.MenuCache;
import com.lap.auth.adapter.driving.persistence.menu.mapper.MenuApiMapper;
import com.lap.auth.adapter.driving.persistence.menu.po.MenuApiPo;
import com.lap.auth.domain.menu.repository.MenuApiRepository;
import com.lap.auth.domain.menu.valueobject.MenuApi;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Repository;

@RequiredArgsConstructor
@Repository
public class MenuApiRepositoryImpl implements MenuApiRepository {

  private final MenuApiMapper menuApiMapper;

  @CacheEvict(
      value = {MenuCache.MENU_OPT, ApiCache.API},
      allEntries = true)
  @Override
  public void saveAll(List<MenuApi> relations) {
    List<MenuApiPo> list =
        relations.stream().map(menu -> new MenuApiPo(menu.menuId(), menu.code())).toList();
    menuApiMapper.insertBatch(list);
  }

  @CacheEvict(
      value = {MenuCache.MENU_OPT, ApiCache.API},
      allEntries = true)
  @Override
  public void deleteByMenuId(Integer menuId) {
    menuApiMapper.deleteByMenuId(menuId);
  }

  @Override
  public List<String> findByMenuId(Integer menuId) {
    return menuApiMapper.selectByMenuId(menuId);
  }
}
